package com.ssy.lingxi.product.serviceImpl.customer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.product.api.model.request.SimpleIdListRequest;
import com.ssy.lingxi.product.api.model.response.BrandResponse;
import com.ssy.lingxi.product.common.tree.AttributeNode;
import com.ssy.lingxi.product.entity.commodity.Commodity;
import com.ssy.lingxi.product.entity.commodity.CommodityAttribute;
import com.ssy.lingxi.product.entity.customer.CustomerAttribute;
import com.ssy.lingxi.product.entity.customer.CustomerAttributeGroup;
import com.ssy.lingxi.product.entity.customer.CustomerAttributeValue;
import com.ssy.lingxi.product.entity.customer.CustomerCategory;
import com.ssy.lingxi.product.entity.platform.Attribute;
import com.ssy.lingxi.product.model.request.CommodityGoodsAttributeRequest;
import com.ssy.lingxi.product.model.resp.CommodityGoodsAttributeAndBrandResponse;
import com.ssy.lingxi.product.model.resp.CommodityGoodsAttributeResponse;
import com.ssy.lingxi.product.repository.commodity.CommodityAttributeRepository;
import com.ssy.lingxi.product.repository.commodity.CommodityGoodsAttributeRepository;
import com.ssy.lingxi.product.repository.commodity.CommodityRepository;
import com.ssy.lingxi.product.repository.customer.CustomerAttributeGroupRepository;
import com.ssy.lingxi.product.repository.customer.CustomerAttributeRepository;
import com.ssy.lingxi.product.repository.customer.CustomerAttributeValueRepository;
import com.ssy.lingxi.product.repository.customer.CustomerCategoryRepository;
import com.ssy.lingxi.product.repository.material.MaterialGroupRepository;
import com.ssy.lingxi.product.repository.platform.AttributeRepository;
import com.ssy.lingxi.product.service.IBrandService;
import com.ssy.lingxi.product.service.customer.ICustomerAttributeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 属性实现类
 *
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/22
 */
@Service
public class CustomerAttributeServiceImpl implements ICustomerAttributeService {

    @Resource
    private CustomerAttributeRepository customerAttributeRepository;

    @Resource
    private CustomerAttributeValueRepository customerAttributeValueRepository;

    @Resource
    private CustomerAttributeGroupRepository customerAttributeGroupRepository;

    @Resource
    private AttributeRepository attributeRepository;

    @Resource
    private CommodityAttributeRepository commodityAttributeRepository;

    @Resource
    private CustomerCategoryRepository customerCategoryRepository;

    @Resource
    private CommodityGoodsAttributeRepository commodityGoodsAttributeRepository;

    @Resource
    private IBrandService brandService;

    @Resource
    private CommodityRepository commodityRepository;

    @Resource
    private MaterialGroupRepository materialGroupRepository;

    /**
     * 同步平台后台属性
     *
     * @return
     */
    @Transactional
    @Override
    public Boolean syncAttribute(UserLoginCacheDTO sysUser, SimpleIdListRequest simpleIdListRequest) {
        List<Long> attributeIdList = simpleIdListRequest.getIdList();
        List<Attribute> attributeList = attributeRepository.findByIdIn(attributeIdList);
        List<String> attributeNameList = attributeList.stream().map(Attribute::getName).collect(Collectors.toList());
        //判断名称是否出现重复
        List<CustomerAttribute> nameList = customerAttributeRepository.findByMemberIdAndMemberRoleIdAndNameIn(sysUser.getMemberId(), sysUser.getMemberRoleId(), attributeNameList);
        if (nameList != null && !nameList.isEmpty()) {
            throw new BusinessException(ResponseCode.PRODUCT_SYNC_ATTRIBUTE_NAME_EXIST);
        }

        HashMap<String, Long> map = new HashMap<>();
        List<CustomerAttribute> customerAttributeList = attributeList.stream().map(attribute -> {
            CustomerAttribute customerAttribute = new CustomerAttribute();
            customerAttribute.setAttribute(attribute);
            customerAttribute.setName(attribute.getName());
            customerAttribute.setIsArea(attribute.getIsArea());
            customerAttribute.setIsDate(attribute.getIsDate());
            customerAttribute.setIsEmpty(attribute.getIsEmpty());
            customerAttribute.setIsEnable(attribute.getIsEnable());
            customerAttribute.setIsPrice(attribute.getIsPrice());
            customerAttribute.setIsSearch(attribute.getIsSearch());
            customerAttribute.setType(attribute.getType());
            customerAttribute.setMemberId(sysUser.getMemberId());
            customerAttribute.setMemberName(sysUser.getCompany());
            customerAttribute.setMemberRoleId(sysUser.getMemberRoleId());
            customerAttribute.setMemberRoleName(sysUser.getMemberRoleName());
            customerAttribute.setUserId(sysUser.getUserId());
            customerAttribute.setUserName(sysUser.getUserName());
            customerAttribute.setCreateTime(System.currentTimeMillis());

            //判断属性组名是否存在，不存在则新增
            String groupName = attribute.getGroupName();
            Long groupId = map.get(groupName);
            if (groupId != null && groupId > 0) {
                customerAttribute.setGroupId(groupId);
                customerAttribute.setGroupName(groupName);
            } else {
                CustomerAttributeGroup customerAttributeGroup = customerAttributeGroupRepository.findFirstByMemberIdAndMemberRoleIdAndName(sysUser.getMemberId(), sysUser.getMemberRoleId(), groupName);
                if (customerAttributeGroup == null) {
                    customerAttributeGroup = new CustomerAttributeGroup();
                    customerAttributeGroup.setName(groupName);
                    customerAttributeGroup.setMemberId(sysUser.getMemberId());
                    customerAttributeGroup.setMemberName(sysUser.getCompany());
                    customerAttributeGroup.setMemberRoleId(sysUser.getMemberRoleId());
                    customerAttributeGroup.setMemberRoleName(sysUser.getMemberRoleName());
                    customerAttributeGroup.setUserId(sysUser.getUserId());
                    customerAttributeGroup.setUserName(sysUser.getUserName());
                    customerAttributeGroupRepository.saveAndFlush(customerAttributeGroup);
                }
                customerAttribute.setGroupId(customerAttributeGroup.getId());
                customerAttribute.setGroupName(customerAttributeGroup.getName());
                map.put(customerAttributeGroup.getName(), customerAttributeGroup.getId());
            }
            return customerAttribute;
        }).collect(Collectors.toList());

        customerAttributeRepository.saveAll(customerAttributeList);
        return true;
    }

    /**
     * 查询属性树
     *
     * @return Wrapper<?>
     */
    @Override
    public List<AttributeNode> getCustomerAttributeTree(UserLoginCacheDTO sysUser, Boolean filterInput) {
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        //一次性查出所有数据
        List<CustomerAttribute> customerAttributeList;
        if (filterInput != null && filterInput) {
            customerAttributeList = customerAttributeRepository.findByMemberIdAndMemberRoleIdAndIsEnableAndTypeNot(memberId, memberRoleId, true, 3);
        } else {
            customerAttributeList = customerAttributeRepository.findByMemberIdAndMemberRoleIdAndIsEnable(memberId, memberRoleId, true);
        }
        List<CustomerAttributeGroup> customerAttributeGroupList = customerAttributeGroupRepository.findByMemberIdAndMemberRoleId(memberId, memberRoleId);

        List<AttributeNode> nodeList = new ArrayList<>();
        customerAttributeGroupList.forEach(attributeGroup -> {
            Long id = attributeGroup.getId();
            AttributeNode node = new AttributeNode();
            node.setId("g_" + id);
            node.setTitle(attributeGroup.getName());
            List<AttributeNode> childrenNodeList = treeChildren(customerAttributeList, id);
            if (!childrenNodeList.isEmpty()) {
                node.setChildren(childrenNodeList);
                nodeList.add(node);
            }
        });
        return nodeList;
    }

    /**
     * 生成子树
     *
     * @param customerAttributeList
     * @param id
     * @return
     */
    private List<AttributeNode> treeChildren(List<CustomerAttribute> customerAttributeList, Long id) {
        List<AttributeNode> nodeList = new ArrayList<>();
        customerAttributeList.forEach(customerAttribute -> {
            Long parentId = customerAttribute.getGroupId();
            if (id.longValue() == parentId.longValue()) {
                AttributeNode node = new AttributeNode();
                node.setId("a_" + customerAttribute.getId());
                node.setTitle(customerAttribute.getName());
                node.setType(0);
                Boolean isDate = customerAttribute.getIsDate();
                if (isDate != null && isDate) {
                    node.setType(1);
                }
                Boolean isArea = customerAttribute.getIsArea();
                if (isArea != null && isArea) {
                    node.setType(2);
                }
                nodeList.add(node);
            }
        });
        return nodeList;
    }

    /**
     * 查询属性
     *
     * @param pageVO
     * @param name
     * @return Page<Attribute>
     */
    @Override
    public Page<CustomerAttribute> getCustomerAttributeList(UserLoginCacheDTO sysUser, PageVO pageVO, String name) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize());
        return customerAttributeRepository.findByMemberIdAndMemberRoleIdAndNameContainingOrderByIdDesc(sysUser.getMemberId(), sysUser.getMemberRoleId(), name, page);
    }

    /**
     * 查询属性
     *
     * @return id
     */
    @Override
    public CustomerAttribute getCustomerAttribute(UserLoginCacheDTO sysUser, Long id) {
        return customerAttributeRepository.findById(id).orElse(null);
    }

    /**
     * 添加/修改属性
     *
     * @param customerAttribute
     * @return
     * @throws Exception
     */
    @Override
    public String saveOrUpdateCustomerAttribute(UserLoginCacheDTO sysUser, CustomerAttribute customerAttribute) {
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        Long userId = sysUser.getUserId();
        Long id = customerAttribute.getId();
        String name = customerAttribute.getName();
        List<CustomerAttribute> old_customerAttribute;
        //判断属性名是否重复
        if (id != null && id > 0) {
            CustomerAttribute currentCustomerAttribute = customerAttributeRepository.findById(id).orElse(null);
            if (currentCustomerAttribute == null) {
                throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_NOT_EXIST);
            }
            old_customerAttribute = customerAttributeRepository.findByMemberIdAndMemberRoleIdAndNameAndIdNot(memberId, memberRoleId, name, id);
        } else {
            old_customerAttribute = customerAttributeRepository.findByMemberIdAndMemberRoleIdAndName(memberId, memberRoleId, name);
        }
        if (!old_customerAttribute.isEmpty()) {
            throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_NAME_EXIST);
        }
        //判断属性组名是否存在，不存在则新增
        String groupName = customerAttribute.getGroupName();
        CustomerAttributeGroup customerAttributeGroup = customerAttributeGroupRepository.findFirstByMemberIdAndMemberRoleIdAndName(memberId, memberRoleId, groupName);
        if (customerAttributeGroup == null) {
            customerAttributeGroup = new CustomerAttributeGroup();
            customerAttributeGroup.setName(groupName);
            customerAttributeGroup.setMemberId(memberId);
            customerAttributeGroup.setMemberName(sysUser.getCompany());
            customerAttributeGroup.setMemberRoleId(memberRoleId);
            customerAttributeGroup.setMemberRoleName(sysUser.getMemberRoleName());
            customerAttributeGroup.setUserId(userId);
            customerAttributeGroup.setUserName(sysUser.getUserName());
            customerAttributeGroupRepository.saveAndFlush(customerAttributeGroup);
        }
        customerAttribute.setGroupId(customerAttributeGroup.getId());
        //检验平台属性是否存在并启用
        Attribute attribute = customerAttribute.getAttribute();
        if (attribute != null) {
            Long attributeId = attribute.getId();
            if (attributeId != null && attributeId > 0) {
                attribute = attributeRepository.findById(attributeId).orElse(null);
                if (attribute != null) {
                    Boolean isEnable = attribute.getIsEnable();
                    if (!isEnable) {
                        throw new BusinessException(ResponseCode.PRODUCT_PLATFORM_ATTRIBUTE_NOT_ENABLE);
                    }
                } else {
                    throw new BusinessException(ResponseCode.PRODUCT_PLATFORM_ATTRIBUTE_NOT_EXIST);
                }
                customerAttribute.setAttribute(attribute);
            }
        }
        customerAttribute.setMemberId(memberId);
        customerAttribute.setMemberName(sysUser.getCompany());
        customerAttribute.setMemberRoleId(memberRoleId);
        customerAttribute.setMemberRoleName(sysUser.getMemberRoleName());
        customerAttribute.setUserId(userId);
        customerAttribute.setUserName(sysUser.getUserName());
        //数据库持久化对象
        customerAttributeRepository.saveAndFlush(customerAttribute);
        return ResponseCode.SUCCESS.getMessage();
    }

    /**
     * 删除属性
     *
     * @param id
     * @return
     */
    @Override
    public String deleteCustomerAttribute(UserLoginCacheDTO sysUser, Long id) {
        //验证数据库中是否存在该数据
        CustomerAttribute customerAttribute = customerAttributeRepository.findById(id).orElse(null);
        if (customerAttribute != null) {
            Boolean isEnable = customerAttribute.getIsEnable();
            if (!isEnable) {
                customerAttributeRepository.deleteById(id);
                return ResponseCode.SUCCESS.getMessage();
            } else {
                throw new BusinessException(ResponseCode.PRODUCT_ENABLE_NOT_DELETE);
            }
        }
        throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_NOT_EXIST);
    }

    /**
     * 启用/停用属性
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public String updateCustomerAttributeStatus(UserLoginCacheDTO sysUser, Long id, Boolean status) {
        Long memberId = sysUser.getMemberId();
        Long memberRoleId = sysUser.getMemberRoleId();
        //验证数据库中是否存在该数据
        CustomerAttribute customerAttribute = customerAttributeRepository.findById(id).orElse(null);
        if (customerAttribute != null) {
            if (!status) {
                //判断是否存在品类关联
                List<CustomerCategory> customerCategoryList = customerCategoryRepository.findByCustomerAttributeListId(customerAttribute.getId());
                if (customerCategoryList != null && customerCategoryList.size() > 0) {
                    throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_HAVE_CATEGORY_NOT_DELETE);
                }

                //判断是否存在属性值
                List<CustomerAttributeValue> customerAttributeValueList = customerAttributeValueRepository.findByMemberIdAndMemberRoleIdAndCustomerAttributeId(memberId, memberRoleId, id);
                if (!customerAttributeValueList.isEmpty()) {
                    throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_VALUE_NOT_STOP);
                }
                //判断是否有商品还在使用
                List<CommodityAttribute> commodityAttributeList = commodityAttributeRepository.findByCustomerAttributeId(id);
                if (!commodityAttributeList.isEmpty()) {
                    throw new BusinessException(ResponseCode.PRODUCT_COMMODITY_RELATION_NOT_STOP);
                }
            }

            //持久化数据
            customerAttribute.setIsEnable(status);
            customerAttributeRepository.saveAndFlush(customerAttribute);
            return ResponseCode.SUCCESS.getMessage();
        } else {
            throw new BusinessException(ResponseCode.PRODUCT_ATTRIBUTE_NOT_EXIST);
        }
    }

    /**
     * 获取可选的属性
     *
     * @param commodityGoodsAttributeRequest
     * @return
     */
    @Override
    public CommodityGoodsAttributeAndBrandResponse getEffectiveAttribute(CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        setSearchParamFromBrand(commodityGoodsAttributeRequest);
        List<Map<String, Long>> commodityGoodsAttributeMapList = findByCondition(commodityGoodsAttributeRequest);
        Map<Long, List<Long>> map = buildMap(commodityGoodsAttributeMapList);
        return buildResult(map, commodityGoodsAttributeRequest);
    }

    /**
     * 根据品牌设置属性参数
     *
     * @param commodityGoodsAttributeRequest
     */
    private void setSearchParamFromBrand(CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        List<Long> brandIdList = commodityGoodsAttributeRequest.getBrandIdList();
        if (CollectionUtil.isEmpty(brandIdList)) {
            //品牌id如果为空就根据品类id查出相关的品牌，不然会全表查询
            brandIdList = findBrandByCategoryId(commodityGoodsAttributeRequest);
        }
        List<Commodity> commodityList = findCommodityByBrandId(brandIdList, commodityGoodsAttributeRequest.getCategoryId());
        List<Long> customerAttributeIdList = getCustomerAttributeId(commodityList);
        setSearchParam(customerAttributeIdList, commodityGoodsAttributeRequest);
    }

    /**
     * 设置查询参数
     *
     * @param customerAttributeIdList
     * @param commodityGoodsAttributeRequest
     */
    private void setSearchParam(List<Long> customerAttributeIdList, CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        List<Long> attributeIdList = commodityGoodsAttributeRequest.getAttributeIdList();
        if (CollectionUtil.isEmpty(attributeIdList)) {
            commodityGoodsAttributeRequest.setAttributeIdList(customerAttributeIdList);
        } else {
            HashSet<Long> idSet = new HashSet<>();
            idSet.addAll(attributeIdList);
            idSet.addAll(customerAttributeIdList);
            commodityGoodsAttributeRequest.setAttributeIdList(new ArrayList<>(idSet));
        }
    }

    /**
     * 获取商品会员属性id
     *
     * @param commodityList
     * @return
     */
    private List<Long> getCustomerAttributeId(List<Commodity> commodityList) {
        return commodityList
                .stream()
                .map(Commodity::getCustomerCategory)
                .filter(Objects::nonNull)
                .map(CustomerCategory::getCustomerAttributeList)
                .filter(CollectionUtil::isNotEmpty)
                .flatMap(List::stream)
                .map(CustomerAttribute::getId)
                .distinct()
                .collect(Collectors.toList());
    }

    /**
     * 根据品牌和品类查商品
     *
     * @param brandIdList
     * @param categoryId
     * @return
     */
    private List<Commodity> findCommodityByBrandId(List<Long> brandIdList, Long categoryId) {
        return commodityRepository.findByCustomerCategoryIdAndBrandIdIn(categoryId, brandIdList);
    }

    /**
     * 根据会员品类id查询品牌id
     *
     * @param commodityGoodsAttributeRequest
     * @return
     */
    private List<Long> findBrandByCategoryId(CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        return brandService.getBrandListByCustomerCategoryId(commodityGoodsAttributeRequest.getCategoryId())
                .stream()
                .map(BrandResponse::getId)
                .collect(Collectors.toList());
    }


    private CommodityGoodsAttributeAndBrandResponse buildResult(Map<Long, List<Long>> map, CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {

        //将map结构转成List结构
        List<CommodityGoodsAttributeResponse> list = getCommodityGoodsAttributeResponseList(map);

        //获取会员属性值id
        List<Long> customerCategoryIdList = getCustomerCategoryIdList(commodityGoodsAttributeRequest);

        //获取会员属性id
        List<Long> customerAttributeIdList = getCustomerAttributeIdList(list);

        //根据会员属性获取品牌id
        List<Long> brandIdList;
        if (CollectionUtil.isNotEmpty(customerCategoryIdList)) {
            brandIdList= getBrandIdList(customerCategoryIdList, customerAttributeIdList);
        }else {
            brandIdList = getBrandIdList(new ArrayList<>(),customerAttributeIdList);
        }


        return buildResult(list, brandIdList);
    }

    private CommodityGoodsAttributeAndBrandResponse buildResult(List<CommodityGoodsAttributeResponse> list, List<Long> brandIdList) {
        CommodityGoodsAttributeAndBrandResponse commodityGoodsAttributeAndBrandResponse = new CommodityGoodsAttributeAndBrandResponse();
        commodityGoodsAttributeAndBrandResponse.setCommodityGoodsAttributeResponseList(list);
        commodityGoodsAttributeAndBrandResponse.setBrandIdList(brandIdList);
        return commodityGoodsAttributeAndBrandResponse;
    }

    /**
     * 根据会员属性获取品牌id
     *
     * @param customerCategoryIdList
     * @param customerAttributeIdList
     * @return
     */
    private List<Long> getBrandIdList(List<Long> customerCategoryIdList, List<Long> customerAttributeIdList) {
        if (CollectionUtil.isEmpty(customerAttributeIdList)) {
            return commodityRepository.findBrandIdListWithCondition(customerCategoryIdList);
        }
        return commodityRepository.findBrandIdListWithCondition(customerAttributeIdList, customerCategoryIdList);
    }

    /**
     * 获取会员属性id
     *
     * @param list
     * @return
     */
    private List<Long> getCustomerAttributeIdList(List<CommodityGoodsAttributeResponse> list) {
        return list
                .stream()
                .map(CommodityGoodsAttributeResponse::getCustomerAttributeId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
    }


    /**
     * 转换成List
     *
     * @param map
     * @return
     */
    private List<CommodityGoodsAttributeResponse> getCommodityGoodsAttributeResponseList(Map<Long, List<Long>> map) {
        List<CommodityGoodsAttributeResponse> list = new ArrayList<>();
        map.forEach((key, value) -> {
            CommodityGoodsAttributeResponse commodityGoodsAttributeResponse = new CommodityGoodsAttributeResponse();
            commodityGoodsAttributeResponse.setCustomerAttributeId(key);
            commodityGoodsAttributeResponse.setCustomerAttributeValueId(value);
            list.add(commodityGoodsAttributeResponse);
        });
        return list;
    }

    /**
     * 获取会员品类id
     *
     * @param commodityGoodsAttributeRequest
     * @return
     */
    private List<Long> getCustomerCategoryIdList(CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        List<Long> customerCategoryIdList;
        if (CollectionUtil.isEmpty(commodityGoodsAttributeRequest.getCategoryIdList())) {
            customerCategoryIdList = new ArrayList<Long>() {{
                add(commodityGoodsAttributeRequest.getCategoryId());
            }};
        } else {
            customerCategoryIdList = commodityGoodsAttributeRequest.getCategoryIdList();
        }
        return customerCategoryIdList;
    }


    private Map<Long, List<Long>> buildMap(List<Map<String, Long>> commodityGoodsAttributeMapList) {
        Map<Long, List<Long>> map = new HashMap<>();
        if (CollectionUtil.isNotEmpty(commodityGoodsAttributeMapList)) {
            commodityGoodsAttributeMapList.forEach(item -> {
                Long customerAttributeId = MapUtil.getLong(item, "customer_attribute_id");
                Long customerAttributeValueId = MapUtil.getLong(item, "customer_attribute_value_id");
                if (map.containsKey(customerAttributeId)) {
                    map.get(customerAttributeId).add(customerAttributeValueId);
                } else {
                    List<Long> list = new ArrayList<>();
                    list.add(customerAttributeValueId);
                    map.put(customerAttributeId, list);
                }
            });
        }
        return map;
    }

    private List<Map<String, Long>> findByCondition(CommodityGoodsAttributeRequest commodityGoodsAttributeRequest) {
        if (CollectionUtil.isEmpty(commodityGoodsAttributeRequest.getCategoryIdList())) {
            commodityGoodsAttributeRequest.setCategoryIdList(new ArrayList<Long>(){{add(commodityGoodsAttributeRequest.getCategoryId());}});
        }
        List<Map<String, Long>> commodityGoodsAttributeMapList;
        if (CollectionUtil.isNotEmpty(commodityGoodsAttributeRequest.getAttributeIdList()) && CollectionUtil.isNotEmpty(commodityGoodsAttributeRequest.getAttributeValueIdList())) {
            commodityGoodsAttributeMapList = commodityGoodsAttributeRepository.findByCustomerAttributeIdAndValueId(commodityGoodsAttributeRequest.getAttributeIdList(), commodityGoodsAttributeRequest.getAttributeValueIdList(),commodityGoodsAttributeRequest.getCategoryIdList());
        } else if (CollectionUtil.isNotEmpty(commodityGoodsAttributeRequest.getAttributeIdList())) {
            commodityGoodsAttributeMapList = commodityGoodsAttributeRepository.findByCustomerAttributeId(commodityGoodsAttributeRequest.getAttributeIdList(),commodityGoodsAttributeRequest.getCategoryIdList());
        } else if (CollectionUtil.isNotEmpty(commodityGoodsAttributeRequest.getAttributeValueIdList())) {
            commodityGoodsAttributeMapList = commodityGoodsAttributeRepository.findByCustomerAttributeIdValueId(commodityGoodsAttributeRequest.getAttributeValueIdList(),commodityGoodsAttributeRequest.getCategoryIdList());
        } else {
            commodityGoodsAttributeMapList = commodityGoodsAttributeRepository.findCommodityGoodsAttribute(commodityGoodsAttributeRequest.getCategoryIdList());
        }
        return commodityGoodsAttributeMapList;
    }

}

